import * as padRecommService from '../services/parentRecommService';
import { fetchChannelTree } from '../services/contentService';
import * as couponService from '../services/memberService';
import { decryptRes, dealPagination } from '../utils/requestUtil';
import { message } from 'antd';

export default {
  namespace: 'parentRecomm',

  state: {
    padRecommList: [],
    padRecommList0: [],
    padRecommList1: [],
    padRecommList2: [],
    padRecommList3: [],
    padRecommList4: [],

    recommPage: {

    },

    goodsList: [],
    channelTree: [],
  },

  effects: {
    *fetchRecommList({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(padRecommService.fetchRecommList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        let data0 = data.filter(item => item.appType == 0)
        let data1 = data.filter(item => item.appType == 1)
        let data2 = data.filter(item => item.appType == 2)
        let data3 = data.filter(item => item.appType == 3)
        let data4 = data.filter(item => item.appType == 4)

        yield put({
          type: 'padRecommReducer',
          payload: { data, data0, data1, data2, data3, data4 },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *changeSts({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(padRecommService.changeSts, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        message.success('操作成功！');
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *addRecommPage({ payload, callback }, { call, put }) {
      console.log('addRecommPage前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(padRecommService.addRecommPage, payload)));
      console.log('addRecommPage接口返回数据：', response);
      let data = response.data;

      if (response.code === 0) {
        if (callback) callback(data);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *deletePadRecomm({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.deletePadRecomm, payload)));

      if (response.code === 0) {
        message.success('删除成功！');
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchRecommPage({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.fetchRecommPage, payload)));
      let data = response.data;

      if (response.code === 0) {
        if (callback) callback(data);
        // yield put({
        //   type: 'recommPageReducer',
        //   payload: { data },
        // });

      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *addRecommPageLayout({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.addRecommPageLayout, payload)));
      let data = response.data;

      if (response.code === 0) {
        message.success('保存成功！');
        yield put({
          type: 'recommPageReducer',
          payload: { data },
        });
        if (callback) callback(data);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *editPageLayout({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.editPageLayout, payload)));
      let data = response.data;

      if (response.code === 0) {
        message.success('保存成功！');
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *deletePageLayout({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.deletePageLayout, payload)));
      let data = response.data;

      if (response.code === 0) {
        message.success('删除成功！');
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchLayoutDetail({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.fetchLayoutDetail, payload)));
      let data = response.data;

      if (response.code === 0) {
        if (callback) callback(data);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *addLayoutDetail({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.addLayoutDetail, payload)));
      let data = response.data;

      if (response.code === 0) {

        if (callback) callback(data);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *editLayoutDetail({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.editLayoutDetail, payload)));
      let data = response.data;

      if (response.code === 0) {

        if (callback) callback(data);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },


    *fetchAdv({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.fetchAdv, payload)));
      if (response.code === 0) {
        let data = response.data;
        if (callback) callback(data);
      } else if (response.code === 2003) {
        const advResponse = JSON.parse(decryptRes(yield call(padRecommService.addAdv, { advName: '广告', advType: payload.advType })));
        if (advResponse.code === 0) {
          const newResponse = JSON.parse(decryptRes(yield call(padRecommService.fetchAdv, payload)));
          if (newResponse.code === 0) {
            let data = newResponse.data;
            if (callback) callback(data);
          }
        }
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *addAdv({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.addAdv, payload)));
      if (response.code === 0) {
        message.success('添加成功！');
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *editAdv({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.addAdv, payload)));
      if (response.code === 0) {
        message.success('添加成功！');
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *addAdvDetail({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.addAdvDetail, payload)));
      if (response.code === 0) {
        message.success('添加成功！');
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *editAdvDetail({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.editAdvDetail, payload)));
      if (response.code === 0) {
        message.success('添加成功！');
        if (callback) callback();
      } else {
        message.error(response.msg);
      }
    },



    *fetchGoodsList({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(couponService.fetchGoodsList, payload)));
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'goodsListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchChannelTree({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(fetchChannelTree, payload)));
      if (response.code === 0) {
        let data = response.data;
        let channelTree = [];
        let one = null;

        for (let i = 0; i < data.length; i++) {
          one = {};
          one["title"] = (data[i].appType == '1' ? '小萌早教' :
            data[i].appType == '2' ? '小萌绘本' : data[i].appType == '3' ? '萌萌乐视频' :
              data[i].appType == '4' ? '萌萌乐儿歌' :
                '其他');
          one['key'] = '' + data[i].appType;
          one['value'] = '' + data[i].appType;
          one['disableCheckbox'] = true;

          if (data[i].channelList && (data[i].channelList.length > 0)) {
            let children = [];
            for (let j = 0; j < data[i].channelList.length; j++) {
              children.push({ "title": data[i].channelList[j].channelName, "key": '' + data[i].channelList[j].channelId, "value": '' + data[i].channelList[j].channelId });
            }
            if (children && children.length > 0) {
              one['children'] = children;
            }
          }
          if (one) {
            channelTree.push(one)
          }
        }

        yield put({
          type: 'channelTreeReducer',
          payload: { data: channelTree },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetch({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.fetch, payload)));
      let data = response.data;

      if (response.code === 0) {
        if (callback) callback(data);
        // yield put({
        //   type: 'recommPageReducer',
        //   payload: { data },
        // });

      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *update({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.update, payload)));
      let data = response.data;
      if (response.code === 0) {
        if (callback) callback(data);
        // yield put({
        //   type: 'recommPageReducer',
        //   payload: { data },
        // });

      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *add({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.add, payload)));
      let data = response.data;
      if (response.code === 0) {
        if (callback) callback(data);
        // yield put({
        //   type: 'recommPageReducer',
        //   payload: { data },
        // });

      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
    *del({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(padRecommService.del, payload)));
      let data = response.data;
      if (response.code === 0) {
        if (callback) callback(data);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
  },

  reducers: {
    padRecommReducer(state, action) {
      return {
        ...state,
        padRecommList: action.payload.data,
        padRecommList0: action.payload.data0,
        padRecommList1: action.payload.data1,
        padRecommList2: action.payload.data2,
        padRecommList3: action.payload.data3,
        padRecommList4: action.payload.data4,
      };
    },

    recommPageReducer(state, action) {
      return {
        ...state,
        recommPage: action.payload.data
      };
    },

    goodsListReducer(state, action) {
      return {
        ...state,
        goodsList: action.payload.data,
      };
    },
    channelTreeReducer(state, action) {
      return {
        ...state,
        channelTree: action.payload.data,
      };
    },

  },
};
